package controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import pojo.Customer;
import pojo.User;


//public class TestController implements Controller {
//	@Override
//	public ModelAndView handleRequest(HttpServletRequest arg0,
//			HttpServletResponse arg1) throws Exception {
//		//get parameter
//		//type transfer
//		//verify
//		//package
//		
//		// call service
//		String uname = "jason";
//		ModelAndView view = new ModelAndView("suc");
//		view.addObject("uname",uname);
//		return view;
//	}
//
//}

@Controller
//@RequestMapping("users") //默认是"/"，项目当前结构下
public class TestController{
////	@RequestMapping(value={"/test1","test11"}) // "user/**/test*"
//	@RequestMapping(value={"/test2"},method={RequestMethod.POST,RequestMethod.PUT})
//	public String test1(Model model){
//		model.addAttribute("uname", "jason1");
//		return "suc";
//	}
//	
////	@RequestMapping("/test2")
//	@RequestMapping(value={"/test2"},method=RequestMethod.GET)
//	public String test2(Model model){
//		model.addAttribute("uname", "jason2");
//		return "suc";
//	}
//	
////	@RequestMapping(value={"/test1"},headers={"Content-Type=application/json"}) //text/html, text/plain, application/json, application/xml, image/jpeg
//	@RequestMapping(value={"/test1"},consumes="application/json",produces="application/json")
//	public String test3(Model model){
//		model.addAttribute("uname", "Test3");
//		return "suc";
//	}
//	
//	@RequestMapping(value={"/test1"},params={"id=1"})
//	public String test4(Model model){
//		model.addAttribute("uname", "Test4");
//		return "suc";
//	}
	
	
	
	
//	@RequestMapping(value="/test2")
//	public String test4(/*@RequestParam("u_name")*/String uname, int age,@DateTimeFormat(pattern="yyyy-MM-dd") Date birth, Model model){
//		//数值，bool,日期类型是自动转换的
//		age++;
//		System.out.println(birth);
//		model.addAttribute("uname", uname);
//		model.addAttribute("age", age);
//		return "suc";
//	}
	
	
	
	@RequestMapping(value="/test3")
	public String test4(User user, Model model){
		System.out.println(user.getName()+"\t"+user.getAge()+"\t"+user.getBirth());
		return "suc";
	}
	
	
	
	@RequestMapping(value="/test4")
	public String test5(@CookieValue(value="JSESSIONID") String key){
		System.out.println("key:"+key);
		return "suc";
	}
	
	@RequestMapping(value="/users/{uid}/books/{bid}")
	public String test6(@PathVariable(value="uid") String uid,@PathVariable int bid){
		System.out.println("uid:"+uid+"\tbid:"+bid);
		return "suc";
	}
	
	@ResponseBody
	@RequestMapping(value="/test5",consumes="application/json")
	public User test7(@RequestBody User user, Model model){
		System.out.println(user.getName()+"\t"+user.getAge()+"\t"+user.getBirth());
		
		return user;
	}
	
	
	@RequestMapping(value="/test6", method={RequestMethod.POST})
	public String test8(Customer customer, Model model){
		System.out.println(customer.getName()+customer.getSal()+customer.getBirth());
		model.addAttribute("customer", customer);
		
		return "result";
	}
	
}

